# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
import pymongo



class LianjiaPipeline(object):
    def process_item(self, item, spider):
        print('房屋信息:', item['houseinfo'])
        print('房屋链接:', item['houseurl'])
        print('房屋位置:', item['housedizhi'])
        # print('房屋详细信息:', item['housexiangxi'])
        print('房屋总价:', item['houseprice'], '万')
        print('平方米价格:', item['houseperprice'])
        print('====' * 10)
        return item

    def __init__(self, mongo_uri, mongo_db):
        self.mongo_uri = mongo_uri
        self.mongo_db = mongo_db

    @classmethod
    def from_crawler(cls, crawler):

        return cls(mongo_uri=crawler.settings.get('MONGO_URI'),
                   mongo_db=crawler.settings.get('MONGO_DB'))

    def open_spider(self, spider):
        self.client = pymongo.MongoClient(self.mongo_uri)
        self.db = self.client[self.mongo_db]

    def process_item(self,item,spider):
        name = item.__class__.__name__
        self.db[name].insert(dict(item))

    def close_spider(self, spoder):
        self.client.close()
